package com.isoftstone.storage.events;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * 
 */
public final class SessionEventSupport {
	/**
	 * 已注册的监听者
	 */
	private final List<SessionListener> listeners = new ArrayList<SessionListener>();

	public void addSessionListener(final SessionListener listener) {
		if (listener != null) {
			listeners.add(listener);
		}
	}

	public void removeSessionListener(final SessionListener listener) {
		listeners.remove(listener);
	}

	public boolean hasSessionListener(final SessionListener listener) {
		return listeners.contains(listener);
	}

	public void fireSessionDisconnected(final SessionEvent sessionEvent) {
		for (Iterator<SessionListener> iter = listeners.iterator(); iter
				.hasNext();) {
			final SessionListener listener = iter.next();
			listener.sessionDisconnected(sessionEvent);
		}
	}

	public void fireSessionDisconnecting(final SessionEvent sessionEvent) {
		for (Iterator<SessionListener> iter = listeners.iterator(); iter
				.hasNext();) {
			final SessionListener listener = iter.next();
			listener.sessionDisconnecting(sessionEvent);
		}
	}

	public void fireSessionLoggedIn(final SessionEvent sessionEvent) {
		for (Iterator<SessionListener> iter = listeners.iterator(); iter
				.hasNext();) {
			final SessionListener listener = iter.next();
			listener.sessionLoggedIn(sessionEvent);
		}
	}

	public void fireSessionLoggedOff(final SessionEvent sessionEvent) {
		for (Iterator<SessionListener> iter = listeners.iterator(); iter
				.hasNext();) {
			final SessionListener listener = iter.next();
			listener.sessionLoggedOff(sessionEvent);
		}
	}

	public void fireSessionOpened(final SessionEvent sessionEvent) {
		for (Iterator<SessionListener> iter = listeners.iterator(); iter
				.hasNext();) {
			final SessionListener listener = iter.next();
			listener.sessionOpened(sessionEvent);
		}
	}

	public void fireSessionOpening(final SessionEvent sessionEvent) {
		for (Iterator<SessionListener> iter = listeners.iterator(); iter
				.hasNext();) {
			final SessionListener listener = iter.next();
			listener.sessionOpening(sessionEvent);
		}
	}

	public void fireSessionConnectionRefused(final SessionEvent sessionEvent) {
		for (Iterator<SessionListener> iter = listeners.iterator(); iter
				.hasNext();) {
			final SessionListener listener = iter.next();
			listener.sessionConnectionRefused(sessionEvent);
		}
	}

	public void fireDebug(final String message) {
		for (Iterator<SessionListener> iter = listeners.iterator(); iter
				.hasNext();) {
			final SessionListener listener = iter.next();
			listener.debug(message);
		}
	}

	public void fireSessionError(final SessionEvent sessionEvent) {
		for (Iterator<SessionListener> iter = listeners.iterator(); iter
				.hasNext();) {
			final SessionListener listener = iter.next();
			listener.sessionError(sessionEvent);
		}
	}
}
